package 
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.Shape;
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.ui.Keyboard;	
	import flash.net.URLRequest;
	import flash.display.Loader;
	import Preload;

	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundMixer;
	
	/**
	 * @author Vincenzo Scalzi 
	 * @version Final 1
	 */
	
	[SWF(backgroundColor="0xc0c4ff")]
	public class Main extends Sprite 
	{
		private var pre:Preload = new Preload([
		"http://ld26.vcz.fr/entry/img/title1.png",
		"http://ld26.vcz.fr/entry/img/screen1.png",
		"http://ld26.vcz.fr/entry/img/screen2.png",
		"http://ld26.vcz.fr/entry/img/screen3.png",
		"http://ld26.vcz.fr/entry/img/screen4.png",
		"http://ld26.vcz.fr/entry/img/end.png",
		"http://ld26.vcz.fr/entry/img/text0.png",
		"http://ld26.vcz.fr/entry/img/text1.png",
		"http://ld26.vcz.fr/entry/img/text2.png",
		"http://ld26.vcz.fr/entry/img/text3.png",
		"http://ld26.vcz.fr/entry/img/text4.png",
		"http://ld26.vcz.fr/entry/img/text5.png",
		"http://ld26.vcz.fr/entry/img/text6.png",
		"http://ld26.vcz.fr/entry/img/text7.png",
		"http://ld26.vcz.fr/entry/img/text8.png",
		"http://ld26.vcz.fr/entry/img/text9.png",
		"http://ld26.vcz.fr/entry/img/text10.png",
		"http://ld26.vcz.fr/entry/img/text11.png",
		"http://ld26.vcz.fr/entry/img/text12.png",
		"http://ld26.vcz.fr/entry/img/text13.png",
		"http://ld26.vcz.fr/entry/img/text14.png",
		"http://ld26.vcz.fr/entry/img/text15.png",
		"http://ld26.vcz.fr/entry/img/text16.png",
		"http://ld26.vcz.fr/entry/img/text17.png",
		"http://ld26.vcz.fr/entry/img/text18.png",
		"http://ld26.vcz.fr/entry/img/text19.png",
		"http://ld26.vcz.fr/entry/img/text20.png",
		"http://ld26.vcz.fr/entry/img/text21.png",
		"http://ld26.vcz.fr/entry/img/text22.png",
		"http://ld26.vcz.fr/entry/img/text23.png",
		"http://ld26.vcz.fr/entry/img/text24.png",
		"http://ld26.vcz.fr/entry/img/text25.png",
		"http://ld26.vcz.fr/entry/img/text26.png",
		"http://ld26.vcz.fr/entry/img/text27.png",
		"http://ld26.vcz.fr/entry/img/text28.png",
		"http://ld26.vcz.fr/entry/img/text29.png",
		"http://ld26.vcz.fr/entry/img/text30.png"
		]);
		
		private var sc:SoundChannel = new SoundChannel();
		private var start:Sound = new Sound( new URLRequest("http://ld26.vcz.fr/entry/mus/title.mp3"));
		private var game_music:Sound = new Sound( new URLRequest("http://ld26.vcz.fr/entry/mus/game.mp3"));
		private var ending:Sound = new Sound( new URLRequest("http://ld26.vcz.fr/entry/mus/ending.mp3"));
		
		private var assets:Array = new Array();
		
		private var slide:DisplayObject;
		private var text:DisplayObject;
		
		private var step:Number = 0;
		private var choice:Number = 0;
		
		private var alfa:Number = 1.;
		private var alfa_speed:Number = 0.05;
		
		private var transition_speed:Number = 30;
		
		private var anim:Boolean = false;
		private var anim_step:Number = 0;
		
		private var listening:Number = 0;
		private var music:Boolean = true;
		
		private var end:Boolean = false;
		
		private var DIFF_SLIDE:Number = 0;
		private var DIFF_TEXT:Number = 6 + DIFF_SLIDE;
		
		static private const PRELOAD_BAR_WIDTH:Number = 634;
		static private const PRELOAD_BAR_HEIGHT:Number = 24;
		static private const PRELOAD_BAR_X:Number = 82;
		static private const PRELOAD_BAR_Y:Number = 275;
		
		private var l:Loader = new Loader();
		public function Load():void {
			l.contentLoaderInfo.addEventListener(Event.COMPLETE, LoadComplete);
			l.load(new URLRequest("http://ld26.vcz.fr/entry/img/preload.png"));
		}
		public function LoadComplete(e:Event):void{
			var i:Bitmap = Bitmap(l.content);
			addChildAt(i,0);
		}

		public function Main()
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			
			Load();
			
			stage.scaleMode = "noScale";
			
			//Catching keys
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
			
			// Preload
			pre.addEventListener("preloadComplete", onPreloadComplete);
			pre.addEventListener("preloadProgress", onPreloadProgress);
			
			addEventListener("SLIDEOUT", slide_out);
			addEventListener("SLIDEIN", slide_in);
			addEventListener("FADEOUT", fade_out);
			addEventListener("FADEIN", fade_in);
			
			addEventListener("END", end_game);
			
			var u:Timer = new Timer(40);
			u.start();
			u.addEventListener(TimerEvent.TIMER, update);
		}
		
		private function onPreloadProgress(e:Event):void {
			var child:Shape = new Shape();
            child.graphics.beginFill(0x112233);
            child.graphics.drawRect(PRELOAD_BAR_X, PRELOAD_BAR_Y, getBarWidth(), PRELOAD_BAR_HEIGHT);
            child.graphics.endFill();
            addChild(child);			
			dispatchEvent(new Event("preloadProgress"));
		}
		
		private function onPreloadComplete(e:Event):void {
			while (numChildren > 0)
				removeChildAt(0);
			assets = pre.objects;
			addChild(assets[DIFF_SLIDE]);
			slide = assets[DIFF_SLIDE];
			text = assets[DIFF_TEXT];
			if (music) sc = start.play(); listening = 1;
		}
		
		private function update(e:Event):void {
			if (anim && !end) {
				switch (anim_step) {
					case 1: dispatchEvent(new Event("SLIDEOUT")); break;
					case 2: dispatchEvent(new Event("FADEOUT")); break;
					case 3: dispatchEvent(new Event("FADEIN")); break;
					case 4: dispatchEvent(new Event("SLIDEIN")); break;
				}
				if (step >= 5) {
					SoundMixer.stopAll(); if (music) sc = ending.play(); listening = 3;
				}
			}
		}
		
		private function keyDownHandler(e:KeyboardEvent):void {	
			if (!end && !anim){
				if (step!= 0) {
					if ( e.keyCode == Keyboard.LEFT)
						choose(1);
					else if ( e.keyCode == Keyboard.RIGHT)
						choose(2);
				}
				else if (step == 0 && e.keyCode == Keyboard.SPACE ){
					choose(0);
					SoundMixer.stopAll(); if (music)  sc = game_music.play(0, 99999); listening = 2;
				}
			}
			if (e.keyCode == Keyboard.M) {
				SoundMixer.stopAll();
				if (!music){
					switch (listening) {
						case 1: sc = start.play();
							break;
						case 2: sc = game_music.play(0,99999);
							break;
						case 3: sc = ending.play();
							break;
					}
				}
				music = !music;
			}
		}
		
		private function choose(n:int):void {
			anim = true;
			choice *= 2;
			choice += n;
			nextStep(new Event(""));
		}
		
		private function nextStep(e:Event):void {
			if (step < 5) {
				step ++;
				dispatchEvent(new Event("SLIDEOUT"));
			}
			else {
				dispatchEvent(new Event("END"));
			}
		}
		
		private function slide_out(e:Event):void {
			anim_step = 1;
			if (text != null && text.y + text.height > 0) {
				text.y -= transition_speed;
			}
			else {
				dispatchEvent(new Event("FADEOUT"));
			}
		}
		
		private function fade_out(e:Event):void {
			anim_step = 2;
			if (alfa > 0) {
				alfa -= alfa_speed;
				slide.alpha = alfa;
			}
			else {
				show_step();
				dispatchEvent(new Event("FADEIN"));
			}
		}
		
		private function fade_in(e:Event):void {
			anim_step = 3;
			if (alfa < 1) {
				alfa += alfa_speed;
				slide.alpha = alfa;
			}
			else {
				show_text();
				dispatchEvent(new Event("SLIDEIN"));
			}
		}
		
		private function slide_in(e:Event):void {
			anim_step = 4;
			if ( text!=null && text.y < 0) {
				text.y += transition_speed;
				if (text.y > 0) text.y = 0;
			}
			else
				free_Keys();
		}
		
		private function show_step():void {
			while (numChildren > 0)
				removeChildAt(0);
			addChild(assets[step + DIFF_SLIDE]);
			slide = assets[step + DIFF_SLIDE];
		}
		
		private function show_text():void {
			if (assets[choice + DIFF_TEXT] != null)
				addChild(assets[choice + DIFF_TEXT]);
			text = assets[choice + DIFF_TEXT];
			if (text != null)
				text.y = 0 - text.height;
		}
		
		private function free_Keys():void {
			anim = false;
			anim_step = 0;
		}
		
		private function end_game(e:Event):void {
			end = true;
		}
		
		private function getBarWidth():Number 
		{
			return PRELOAD_BAR_WIDTH * pre.percLoaded / 100;
		}
	}
}